package com.cheer.controller;

import com.cheer.common.Page;
import com.cheer.common.Result;
import com.cheer.dto.OaLearnClassAddDTO;
import com.cheer.dto.OaLearnClassUpdateDTO;
import com.cheer.exception.ServiceException;
import com.cheer.security.UserInfoHolder;
import com.cheer.service.OaAdminService;
import com.cheer.service.OaLearnClassService;
import com.cheer.vo.OaLearnClassInfoVO;
import com.cheer.vo.OaLearnClassListVO;
import com.cheer.vo.SimpleObjectVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/learn/class")
public class OaLearnClassController {

    @Autowired
    private OaLearnClassService oaLearnClassService;

    @Autowired
    private OaAdminService oaAdminService;

    @Autowired
    private UserInfoHolder userInfoHolder;

    @PostMapping("/add")
    public Result add(@RequestBody @Valid OaLearnClassAddDTO learnClassAddDTO) {
        if (learnClassAddDTO.getPid() == null) {
            learnClassAddDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        oaLearnClassService.add(learnClassAddDTO);
        return Result.success();
    }

    @GetMapping("/sub")
    public Result<List<SimpleObjectVO>> sub(@RequestParam Integer id) {
        return Result.success(oaLearnClassService.sub(id));
    }

    @GetMapping("/list")
    public Result<Page<OaLearnClassListVO>> list(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                 @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                 @RequestParam(required = false) Integer id) {
        if (id == null) {
            id = userInfoHolder.getAdmin().getPid();
        }
        return Result.success(oaLearnClassService.list(pageNum, pageSize, id));
    }

    @PostMapping("/download")
    public Result download(@RequestParam(required = false) Integer id, HttpServletResponse response) {
        if (id == null) {
            id = userInfoHolder.getAdmin().getPid();
        }
        oaLearnClassService.download(id, response);
        return Result.success();
    }

    @GetMapping("/delete")
    @Transactional
    public Result delete(@RequestParam Integer id) {
        if (Objects.equals(id, userInfoHolder.getAdmin().getPid())){
            throw new ServiceException("不能删除自己的所属单位");
        }

        oaLearnClassService.delete(id);
        // 删除单位管理员
        oaAdminService.deleteByPid(id);
        return Result.success();
    }

    @GetMapping("/info")
    public Result<OaLearnClassInfoVO> info(@RequestParam Integer id) {
        OaLearnClassInfoVO info = oaLearnClassService.info(id);
        for (int i = 0; i < info.getPath().size(); i++) {
            if (Objects.equals(info.getPath().get(i), userInfoHolder.getAdmin().getPid())) {
                info.setPath(info.getPath().subList(i+1, info.getPath().size()));
                break;
            }
        }
        return Result.success(info);
    }

    @PostMapping("/update")
    public Result update(@RequestBody @Valid OaLearnClassUpdateDTO learnClassUpdateDTO) {
        if (learnClassUpdateDTO.getPid() == null) {
            learnClassUpdateDTO.setPid(userInfoHolder.getAdmin().getPid());
        }
        oaLearnClassService.update(learnClassUpdateDTO);
        return Result.success();
    }

    @PostMapping("/import")
    public Result importLearnClass(Integer pid, MultipartFile file) {
        oaLearnClassService.importLearnClass(pid, file);
        return Result.success();
    }

    @PostMapping("/import/all")
    public Result importAll(MultipartFile file) {
        oaLearnClassService.importAll(file);
        return Result.success();
    }
}
